package com.qiaoyanqing.ximalaya.discovermodule.discover_activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.qiaoyanqing.ximalaya.R;
import com.qiaoyanqing.ximalaya.discovermodule.discover_adapter.DiscoverRankFragmentpagerAdapter;
import com.qiaoyanqing.ximalaya.discovermodule.discover_callback.DiscoverRankSecondService;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.DiscoverRankSecondCategories;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.DiscoverRankSecondEntity;
import com.qiaoyanqing.ximalaya.url.Urlconfig;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class DiscoverRankActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener, Callback<DiscoverRankSecondEntity> {
    private String title;
    private DrawerLayout drawer;
    public int tabPosition;
    private String contentType;
    private String key;
    private int pageSize;
    private List<String>keyList=new ArrayList<>();
    private List<DiscoverRankSecondCategories> categories;
    private Intent intent;
    private ViewPager pager;
    private String[] list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rank);
       initView();

    }

    private void initView() {
        pager=(ViewPager)findViewById(R.id.pager);
        TabLayout tab=(TabLayout)findViewById(R.id.tab);
        Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        categories=new ArrayList<>();
        Bundle bundle=new Bundle();
        intent=getIntent();
        key=intent.getStringExtra("key");
        keyList.add(key);
        title=intent.getStringExtra("title");
        contentType=intent.getStringExtra("contentType");
        ((TextView) findViewById(R.id.title)).setText(title);
        drawer=(DrawerLayout)findViewById(R.id.drawer);
        list=getResources().getStringArray(R.array.rank_second_toolbar_title);

        tab.setupWithViewPager(pager);
        tab.addOnTabSelectedListener(this);
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(Urlconfig.RANK_SECOND_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        DiscoverRankSecondService service=retrofit.create(DiscoverRankSecondService.class);

        service.getDatas(contentType,"android",key,1,pageSize,2).enqueue(this);
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    @Override
    public void onResponse(Call<DiscoverRankSecondEntity> call, Response<DiscoverRankSecondEntity> response) {
        categories = response.body().getCategories();
        for (int i = 0; i < categories.size(); i++) {
            keyList.add(categories.get(i).getKey());
        }
        pager.setAdapter(new DiscoverRankFragmentpagerAdapter(getSupportFragmentManager(),list,keyList));

    }

    @Override
    public void onFailure(Call<DiscoverRankSecondEntity> call, Throwable t) {

    }
    public void onClick(View view){
        finish();
    }
}
